package com.mcc.app.energy.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.app.cache.service.IPlcCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.app.energy.mapper.EnergyPowerStatisticsMapper;
import com.mcc.app.energy.domain.EnergyPowerStatistics;
import com.mcc.app.energy.service.IEnergyPowerStatisticsService;

/**
 * 能源电力耗电统计Service业务层处理
 *
 * @author gxy
 * @date 2025-03-25
 */
@Service
public class EnergyPowerStatisticsServiceImpl implements IEnergyPowerStatisticsService {

    @Autowired
    private EnergyPowerStatisticsMapper energyPowerStatisticsMapper;

    @Autowired
    private IPlcCacheService plcCacheService;

    /**
     * 查询能源电力耗电统计
     *
     * @param id 能源电力耗电统计ID
     * @return 能源电力耗电统计
     */
    @Override
    public EnergyPowerStatistics selectEnergyPowerStatisticsById(Long id) {
        return energyPowerStatisticsMapper.selectById(id);
    }

    /**
     * 查询能源电力耗电统计列表
     *
     * @param energyPowerStatistics 能源电力耗电统计
     * @return 能源电力耗电统计
     */
    @Override
    public List<EnergyPowerStatistics> selectEnergyPowerStatisticsList(EnergyPowerStatistics energyPowerStatistics) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(energyPowerStatistics)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getTime()), "time", energyPowerStatistics.getTime());
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getTableType()), "table_type", energyPowerStatistics.getTableType());
            queryWrapper.like(ObjectUtil.isNotEmpty(energyPowerStatistics.getGroupName()), "group_name", energyPowerStatistics.getGroupName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getGroupNo()), "group_no", energyPowerStatistics.getGroupNo());
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getTransformer1()), "transformer1", energyPowerStatistics.getTransformer1());
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getTransformer2()), "transformer2", energyPowerStatistics.getTransformer2());
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getTransformer3()), "transformer3", energyPowerStatistics.getTransformer3());
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getTransformer4()), "transformer4", energyPowerStatistics.getTransformer4());
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getTransformer5()), "transformer5", energyPowerStatistics.getTransformer5());
            queryWrapper.eq(ObjectUtil.isNotEmpty(energyPowerStatistics.getTransformer6()), "transformer6", energyPowerStatistics.getTransformer6());
        }
        return energyPowerStatisticsMapper.selectList(queryWrapper);
    }

    /**
     * 新增能源电力耗电统计
     *
     * @param energyPowerStatistics 能源电力耗电统计
     * @return 结果
     */
    @Override
    public int insertEnergyPowerStatistics(EnergyPowerStatistics energyPowerStatistics) {
        return energyPowerStatisticsMapper.insert(energyPowerStatistics);
    }

    /**
     * 修改能源电力耗电统计
     *
     * @param energyPowerStatistics 能源电力耗电统计
     * @return 结果
     */
    @Override
    public int updateEnergyPowerStatistics(EnergyPowerStatistics energyPowerStatistics) {
        return energyPowerStatisticsMapper.updateById(energyPowerStatistics);
    }

    /**
     * 删除能源电力耗电统计
     *
     * @param id 能源电力耗电统计ID
     * @return 结果
     */
    @Override
    public int deleteEnergyPowerStatisticsById(Long id) {
        return energyPowerStatisticsMapper.deleteById(id);
    }

    /**
     * 批量删除能源电力耗电统计
     *
     * @param ids 能源电力耗电统计ID
     * @return 结果
     */
    @Override
    public int deleteEnergyPowerStatisticsByIds(Long[] ids) {
        if (ids.length == 0) return 0;
        return energyPowerStatisticsMapper.deleteBatchIds(ListUtil.toList(ids));
    }

    @Override
    public Map<String, Object> getRealTimeInfo(){
        return plcCacheService.getRealDataByTags("变频器","");
    }

    @Override
    public Map<String, List<Map<String, Object>>> areaPowerChart() {
        HashMap<String, List<Map<String, Object>>> resp = new HashMap<>();

        List<Map<String, Object>> areaDayPowerChart = energyPowerStatisticsMapper.getAreaDayPowerChart();
        resp.put("day", areaDayPowerChart);

        List<Map<String, Object>> areaMonthPowerChart = energyPowerStatisticsMapper.getAreaMonthPowerChart();
        resp.put("month", areaMonthPowerChart);

        List<Map<String, Object>> areaYearPowerChart = energyPowerStatisticsMapper.getAreaYearPowerChart();
        resp.put("year", areaYearPowerChart);
        return resp;
    }

}
